# -*- coding: utf-8 -*-

class AreaController < ApplicationMapController
  protect_from_forgery
  
  def index
    @title = "地図で検索"
  end
  
  def search
    #地方の特定
    mc = (params[:id]=="0" ? nil : MasterMapcontroller.find(params[:id]))
    if not mc.nil?
      @latitude = mc.latitude
      @longtitude = mc.longtitude
      @zoom = mc.zoom
    else
      @latitude = 37.0
      @longtitude = 138.0
      @zoom = 5
    end
    #蔵元一覧
    if params[:id]==0
      @makers = Maker.where(:is_show => true).all
    else
      prefectures = Prefecture.where(:district => params[:id]).all
      if prefectures.blank?
        @makers = Maker.where(:is_show => true).all
        return
      end
      prefs_where = "is_show = true"
      prefectures.each do |p|
        prefs_where += prefs_where[-4, 4] == "true" ? " and (" : " or "
        prefs_where += " prefecture = " + p.id.to_s
      end
      prefs_where += ")" if not prefs_where[-4, 4] == "true"
      @makers = Maker.where(prefs_where).all
    end

    @title = "地図で検索"
  end
end
